﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 连接图标与槽函数
    connect(ui->chart_rect,SIGNAL(clicked()),ui->flowChart_widget,SLOT(setPaintChart_Rect())); // 矩形
    connect(ui->chart_diam,SIGNAL(clicked()),ui->flowChart_widget,SLOT(setPaintChart_Diamond())); // 菱形
    connect(ui->chart_roundrect,SIGNAL(clicked()),ui->flowChart_widget,SLOT(setPaintChart_RoundRect())); // 圆角矩形
    connect(ui->chart_ellipse,SIGNAL(clicked()),ui->flowChart_widget,SLOT(setPaintChart_Ellipse())); // 圆形
    connect(ui->chart_line,SIGNAL(clicked()),ui->flowChart_widget,SLOT(setPaintChart_Line())); // 直线
    connect(ui->chart_trapezoid,SIGNAL(clicked()),ui->flowChart_widget,SLOT(setPaintChart_trapezoid())); // 梯形
    connect(ui->chart_pingxing,SIGNAL(clicked()),ui->flowChart_widget,SLOT(setPaintChart_parallel())); // 平行四边形
    connect(ui->chart_liubianxing,SIGNAL(clicked()),ui->flowChart_widget,SLOT(setPaintChart_hexagon())); // 六边形
    connect(ui->chart_person,SIGNAL(clicked()),ui->flowChart_widget,SLOT(setPaintChart_actor())); // 参与者
    connect(ui->chart_shujuku,SIGNAL(clicked()),ui->flowChart_widget,SLOT(setPaintChart_db())); // 参与者

    connect(ui->actionOpenFile,SIGNAL(triggered(bool)),ui->flowChart_widget,SLOT(openChartFile())); // 打开文件
    connect(ui->actionSaveFile,SIGNAL(triggered(bool)),ui->flowChart_widget,SLOT(saveChartFile())); // 保存文件
    connect(ui->actionNewFile,SIGNAL(triggered(bool)),ui->flowChart_widget,SLOT(newChartFile())); // 新建文件

    connect(ui->LineColorButton,SIGNAL(clicked(bool)),this,SLOT(sendSelChartLineColor())); // 线段颜色
    connect(ui->TextColorButton,SIGNAL(clicked(bool)),this,SLOT(sendSelTextColor())); // 文本颜色
    connect(ui->FillColorButton,SIGNAL(clicked(bool)),this,SLOT(sendSelChartFillColor())); // 填充颜色
    connect(ui->LineWidthCBox,SIGNAL(currentIndexChanged(int)),ui->flowChart_widget,SLOT(setLineWidth(int))); // 线段宽度
    connect(ui->LineStartStyleCBox,SIGNAL(currentIndexChanged(int)),ui->flowChart_widget,SLOT(setLineStartStyle(int))); // 线段开头形状
    connect(ui->LineEndStyleCBox,SIGNAL(currentIndexChanged(int)),ui->flowChart_widget,SLOT(setLineEndStyle(int))); // 线段末尾形状

    connect(ui->flowChart_widget,SIGNAL(sendChartStyle(QPen &, QBrush &)),this,SLOT(setFillStyle(QPen &, QBrush &)));
    connect(ui->flowChart_widget,SIGNAL(sendLineStyle(QPen &, LINE_HEAD_TYPE &, LINE_HEAD_TYPE &)),this,SLOT(setLineStyle(QPen &, LINE_HEAD_TYPE &, LINE_HEAD_TYPE &)));
    connect(ui->flowChart_widget,SIGNAL(disableStyle()),this,SLOT(disableStylePanel()));
    connect(ui->flowChart_widget,SIGNAL(updateWindowTitle(const QString &)),this,SLOT(changeWindowTitle(const QString &)));


}

MainWindow::~MainWindow()
{
    ui->flowChart_widget->newChartFile();
    delete ui;
}

void MainWindow::setFillStyle(QPen &qp, QBrush &qb)
{
    // 设置图形填充样式
    ui->FillColorGroup->setEnabled(true);
    ui->LineColorGroup->setEnabled(true);
    ui->FillColorButton->setEnabled(true);
    ui->LineColorButton->setEnabled(true);
    ui->LineColorButton->setStyleSheet(QString("QPushButton\
    {\
        background-color:rgb(%1,%2,%3);\
        opacity:1;\
    };"
    ).arg(qp.color().red()).arg(qp.color().green()).arg(qp.color().blue()));
    ui->FillColorButton->setStyleSheet(QString("QPushButton\
    {\
        background-color:rgb(%1,%2,%3);\
        opacity:1;\
    };"
    ).arg(qb.color().red()).arg(qb.color().green()).arg(qb.color().blue()));

}

void MainWindow::setLineStyle(QPen &qp,LINE_HEAD_TYPE &startLineHeadType, LINE_HEAD_TYPE &endLineHeadTypeb)
{
    // 设置线段类型
    ui->LineColorGroup->setEnabled(true);
    ui->LineWidthGroup->setEnabled(true);
    ui->LineStartStyleGroup->setEnabled(true);
    ui->LineEndStyleGroup->setEnabled(true);
    ui->LineColorButton->setEnabled(true);
    ui->LineWidthCBox->setEnabled(true);
    ui->LineStartStyleCBox->setEnabled(true);
    ui->LineEndStyleCBox->setEnabled(true);
    ui->LineColorButton->setStyleSheet(QString("QPlainTextEdit\
    {\
        background-color:rgb(%1,%2,%3);\
        opacity:1;\
    }"
    ).arg(qp.color().red()).arg(qp.color().green()).arg(qp.color().blue()));
    ui->LineWidthCBox->setCurrentIndex(qp.width());
    ui->LineStartStyleCBox->setCurrentIndex(int(startLineHeadType)); // 设置起始线段样式
    ui->LineEndStyleCBox->setCurrentIndex(int(endLineHeadTypeb));    // 设置末尾线段样式
}

void MainWindow::disableStylePanel()
{
    // 设置某项功能为不激活样式
    ui->FillColorGroup->setEnabled(false); // 填充颜色
    ui->LineColorGroup->setEnabled(false); // 线段颜色
    ui->LineWidthGroup->setEnabled(false); // 线段宽度
    ui->LineStartStyleGroup->setEnabled(false); // 线段开头形状
    ui->LineEndStyleGroup->setEnabled(false); // 线段末尾形状
    ui->TextColorGroup->setEnabled(false); // 文本颜色
    ui->LineColorButton->setStyleSheet(QString("QPushButton\
    {\
        opacity:0.9;\
    };"));
    ui->FillColorButton->setStyleSheet(QString("QPushButton\
    {\
        opacity:0.9;\
    };"));
}

void MainWindow::sendSelChartLineColor()
{

    QColor color = QColorDialog::getColor(Qt::white,this,tr("设置线条颜色"));
    ui->flowChart_widget->setSelChartLineColor(color); // 设置线条颜色
    ui->LineColorButton->setStyleSheet(QString("QPushButton\
    {\
        background-color:rgb(%1,%2,%3);\
        opacity:1;\
    };"
    ).arg(color.red()).arg(color.green()).arg(color.blue()));
}

void MainWindow::sendSelChartFillColor()
{

    QColor color = QColorDialog::getColor(Qt::white,this,tr("设置填充颜色"));
    ui->flowChart_widget->setSelChartFillColor(color); // 设置填充颜色
    ui->FillColorButton->setStyleSheet(QString("QPushButton\
    {\
        background-color:rgb(%1,%2,%3);\
        opacity:1;\
    };"
    ).arg(color.red()).arg(color.green()).arg(color.blue()));
}
void MainWindow::sendSelTextColor()
{
    QColor color = QColorDialog::getColor(Qt::white,this,tr("设置文本颜色"));
    ui->flowChart_widget->setSelChartTextColor(color); // 设置文本颜色
    ui->TextColorButton->setStyleSheet(QString("QPushButton\
    {\
        background-color:rgb(%1,%2,%3);\
        opacity:1;\
    };"
    ).arg(color.red()).arg(color.green()).arg(color.blue()));
}
void MainWindow::changeWindowTitle(const QString & title)
{
    QString tmp = QString("流程图 - %1").arg(title);
    this->setWindowTitle(tmp);
}

